
import numpy as np
from scipy.stats import gaussian_kde

class EntropyMetrics:
    """信息熵度量机制"""
    def __init__(self, model_dim):
        self.model_dim = model_dim  # 模型参数量
        
    def gradient_entropy(self, gradients):
        """参数分布熵建模 (式5)"""
        # 使用核密度估计计算概率密度
        kde = gaussian_kde(gradients.flatten())
        density = kde(gradients.flatten())
        density = density / (density.sum() + 1e-10)
        entropy = -np.sum(density * np.log(density + 1e-10))
        return entropy / self.model_dim
    
    def attention_weight(self, entropies, temperature):
        """熵驱动的注意力权重分配 (式6)"""
        weights = np.exp(-entropies / temperature)
        return weights / np.sum(weights)
    
    def entropy_change_rate(self, entropy_history, window=5):
        """跨轮次熵演化分析 (式7)"""
        if len(entropy_history) < window:
            return 0
        recent = entropy_history[-window:]
        return np.abs(np.mean(np.diff(recent)))
